/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.purpose.mapper;

import java.util.List;
import java.util.Map;

import com.koron.purpose.bean.ResponsibilitiesLibBean;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.koron.ebs.mybatis.EnvSource;

import com.koron.purpose.bean.JobsTemplateBean;
import com.koron.purpose.domain.JobsTemplate;


public interface JobsTemplateMapper {

    @Insert("insert into jobs_template(id,add_user,add_time,update_user,update_time,add_user_name,update_user_name,corp_code,corp_name,name,code,job_type,note,serial)"
            + "values(#{id},#{addUser},#{addTime},#{updateUser},#{updateTime},#{addUserName},#{updateUserName},#{corpCode},#{corpName},#{name},#{code},#{jobType},#{note},#{serial})")
    int insert(JobsTemplate bean);

    @Insert("<script>" +
            "insert into jobs_template(id,add_user,add_time,update_user,update_time,add_user_name,update_user_name,name,code,job_type,note,serial) VALUES" +
            "<foreach collection='list' item='bean' separator =','>" +
            "(#{bean.id},#{bean.addUser},#{bean.addTime},#{bean.updateUser},#{bean.updateTime},#{bean.addUserName},#{bean.updateUserName},#{bean.name},#{bean.code}," +
            "#{bean.jobType},#{bean.note},#{bean.serial})" +
            "</foreach>"
            + "</script>")
    Integer batchInsertJobsTemplate(@Param("list") List<JobsTemplate> list);

    @Update("update jobs_template set update_user=#{updateUser},update_time=#{updateTime},update_user_name=#{updateUserName},"
            + "corp_code=#{corpCode},corp_name=#{corpName},name=#{name},code=#{code},job_type=#{jobType},note=#{note},serial=#{serial} where id=#{id}")
    int update(JobsTemplate bean);

    @Delete("delete from jobs_template where id=#{id}")
    int delete(@Param("id") String id);

    @Select("select * from jobs_template where id=#{id}")
    JobsTemplateBean queryById(@Param("id") String id);

    @Select("<script>" +
            "select count(*) from jobs_template where 1=1" +
            " <if test='corpCode!=null and !corpCode.isEmpty()'> and corp_code = #{corpCode} </if>" +
            " <if test='corpName!=null and !corpName.isEmpty()'> and corp_name like CONCAT('%',#{corpName},'%') </if>" +
            " <if test='name!=null and !name.isEmpty()'> and name like #{name} </if>" +
            "</script>")
    int count(@Param("corpCode") String corpCode, @Param("corpName") String corpName, @Param("name") String name);

    @Select("<script>"
            + "select a.*, b.dictLabel from jobs_template a " +
            "left join (SELECT tb.id, tb.dict_label AS dictLabel from dict_data tb where tb.dict_key ='jobs_template_job_type') b " +
            "on a.job_type = b.id where 1=1 "
            + "<if test='corpCode!=null and !corpCode.isEmpty()'> and a.corp_code = #{corpCode} </if>"
            + "<if test='corpName!=null and !corpName.isEmpty()'> and a.corp_name like CONCAT('%',#{corpName},'%') </if>"
            + "<if test='name!=null'> and a.name like #{name} </if>"
            + "<if test='code!=null'> and a.code like CONCAT('%',#{code},'%') </if>"
            + "<if test='jobType!=null'> and a.job_type like #{jobType} </if>"
            + "order by a.serial"
            + "<if test='start!=null and row!=null'> limit #{start},#{row} </if>"
            + "</script>")
    List<JobsTemplateBean> list(@Param("corpCode") String corpCode, @Param("corpName") String corpName, @Param("name") String name, @Param("code") String code, @Param("jobType") String jobType,
                                @Param("start") Integer start, @Param("row") Integer row);

    @Select("select count(*) from jobs_template where name=#{name}")
    Integer addVerification(@Param("name") String name);

    @Select("select count(*) from jobs_template where name=#{name} and id!=#{id}")
    Integer updateVerification(@Param("name") String name, @Param("id") String id);

    @Select("select b.* from jobs_template_responsibilitie a,responsibilities_lib b " +
            "WHERE a.responsibilities_lib_id = b.id and a.jobs_template_id = #{jobsTemplateId} AND b.`status` = '1'")
    List<ResponsibilitiesLibBean> getResponsibilitiesLibs(String jobsTemplateId);

    @Insert("<script>" +
            "insert into jobs_template_responsibilitie(jobs_template_id, responsibilities_lib_id) VALUES" +
            "<foreach collection='rls' item='bean' separator =','>" +
            "(#{bean.jobsTemplateId}, #{bean.responsibilitiesLibId})" +
            "</foreach>"
            + "</script>")
    Integer batchInsertRL(@Param("rls") List<Map> rls);

    @Delete("delete from jobs_template_responsibilitie where jobs_template_id = #{jobsTemplateId}")
    Integer deleteRLByTemplateId(String jobsTemplateId);
}
